package com.co.video.service.impl;

import com.co.video.mapper.CategoryMapper;
import com.co.video.pojo.Category;
import com.co.video.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * GlmCategory业务层接口实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * GlmCategory条件+分页查询
     *
     * @param category 查询条件
     * @param page     页码
     * @param size     页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Category> findPage(Category category, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(category);
        //执行搜索
        return new PageInfo<Category>(categoryMapper.selectByExample(example));
    }

    /**
     * GlmCategory分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Category> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Category>(categoryMapper.selectAll());
    }

    /**
     * GlmCategory条件查询
     *
     * @param category
     * @return
     */
    @Override
    public List<Category> findList(Category category) {
        //构建查询条件
        Example example = createExample(category);
        //根据构建的条件查询数据
        return categoryMapper.selectByExample(example);
    }


    /**
     * GlmCategory构建查询对象
     *
     * @param category
     * @return
     */
    public Example createExample(Category category) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        if (category != null) {
            // 分类id
            if (!StringUtils.isEmpty(category.getId())) {
                criteria.andEqualTo("id", category.getId());
            }
            // 分类名称
            if (!StringUtils.isEmpty(category.getName())) {
                criteria.andLike("name", "%" + category.getName() + "%");
            }
            // 排序
            if (!StringUtils.isEmpty(category.getSeq())) {
                criteria.andEqualTo("seq", category.getSeq());
            }
            // 上级ID
            if (!StringUtils.isEmpty(category.getParentId())) {
                criteria.andEqualTo("parentId", category.getParentId());
            }
            // 分类数量
            if (!StringUtils.isEmpty(category.getNums())) {
                criteria.andEqualTo("nums", category.getNums());
            }
            // 创建时间
            if (!StringUtils.isEmpty(category.getCreateTime())) {
                criteria.andEqualTo("createTime", category.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(category.getUpdateTime())) {
                criteria.andEqualTo("updateTime", category.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改GlmCategory
     *
     * @param category
     */
    @Override
    public void update(Category category) {
        categoryMapper.updateByPrimaryKey(category);
    }

    /**
     * 增加GlmCategory
     *
     * @param category
     */
    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
    }

    /**
     * 根据ID查询GlmCategory
     *
     * @param id
     * @return
     */
    @Override
    public Category findById(String id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询GlmCategory全部数据
     *
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.selectAll();
    }

    @Override
    public Category findByName(String name) {
        Category category = new Category();
        category.setName(name);
        return categoryMapper.selectOne(category);
    }

    @Override
    public List<Category> findCategory() {
        return categoryMapper.findCategory();
    }

    @Override
    public List<Category> findSubCategory(String parent_id) {
        return categoryMapper.findSubCategory(parent_id);
    }


}
